//
//  RecommendViewModel.swift
//  DouYuZB
//
//  Created by 中创 on 2020/4/22.
//  Copyright © 2020 梁森. All rights reserved.
//

import UIKit

class RecommendViewModel:BaseAnchorViewModel {
    /// 所有的组
//    var anchorGroups:[AnchorGroup] = []
    /// 热门数据
    private lazy var hotGroup:AnchorGroup = AnchorGroup()
    /// 颜值数据
    private lazy var prettyGroup:AnchorGroup = AnchorGroup()
    /// 轮播数据
    var cycleDatas:[CycleModel] = []
}


// MARK:网络请求相关
extension RecommendViewModel{
    func requestData(finishCallBack:@escaping()->()) {
        
        let currentDate = NSDate.getCurrentTime()
        let parameters = ["limit":"4", "offset":"0", "time":currentDate]
        let dispatchGroup = DispatchGroup()
        // 请求热门数据
        dispatchGroup.enter()
        NetworkTool.requestData(url: "http://capi.douyucdn.cn/api/v1/getbigDataRoom", params: ["time":currentDate], method: .GET, success: { (response) in
            guard let responseDict = response as? [String:NSObject] else{return}
            guard let dataArray = responseDict["data"] as? [[String:NSObject]] else{return}
            var room_list:[AnchorModel] = []
            for dict in dataArray{
                let model = AnchorModel.deserialize(from: dict)
                room_list.append(model!)
            }
            self.hotGroup.room_list = room_list
            self.hotGroup.tag_name = "热门"
            self.hotGroup.icon_name = "home_header_hot"
            dispatchGroup.leave()
            print("热门网络请求结束")
        }) { (response) in
            
        }
        // 请求颜值数据
        dispatchGroup.enter()
        let prettyUrlStr = "http://capi.douyucdn.cn/api/v1/getVerticalRoom"
        NetworkTool.requestData(url: prettyUrlStr, params: parameters, method: .GET, success: { (response) in
            guard let responseDict = response as? [String:NSObject] else{return}
            guard let dataArray = responseDict["data"] as? [[String:NSObject]] else{return}
            var room_list:[AnchorModel] = []
            for dict in dataArray{
                let model = AnchorModel.deserialize(from: dict)
                room_list.append(model!)
            }
            self.prettyGroup.room_list = room_list
            self.prettyGroup.tag_name = "颜值"
            self.prettyGroup.icon_name = "home_header_phone"
            print("颜值数据：\(self.prettyGroup.toJSONString() ?? "")")
            dispatchGroup.leave()
            print("颜值网络请求结束")
        }) { (response) in
            
        }
        // 请求多个数据
        dispatchGroup.enter()
//        NetworkTool.requestData(url: "http://capi.douyucdn.cn/api/v1/getHotCate", params: parameters, method: .GET, success: { (response) in
//            guard let responseDict = response as? [String:NSObject] else{return}
//            guard let dataArray = responseDict["data"] as? [[String:NSObject]] else{return}
//            for dict in dataArray{
//                let model = AnchorGroup.deserialize(from: dict)
//                model!.icon_name = "home_header_normal"
//                self.anchorGroups.append(model!)
//            }
//            dispatchGroup.leave()
//        }) { (response) in
//            
//        }
        requestData(url: "http://capi.douyucdn.cn/api/v1/getHotCate", parameters: parameters) {
            dispatchGroup.leave()
        }
        
        // 所有的网络请求结束
        dispatchGroup.notify(queue: DispatchQueue.main) {
            print("三个网络请求都结束...")
            self.anchorGroups.insert(self.prettyGroup, at: 0)
            self.anchorGroups.insert(self.hotGroup, at: 0)
            finishCallBack()
        }
        
    }
    
    /// 请求轮播图数据
    func requestCycleData(finishCallBack:@escaping()->(), errorCallBack:@escaping()->()) {
        let parameters = ["version":"2.300"]
        NetworkTool.requestData(url: "http://www.douyutv.com/api/v1/slide/6", params: parameters, method: .GET, success: { (response) in
            guard let responseDict = response as? [String:NSObject] else{return}
            guard let dataArray = responseDict["data"] as? [[String:NSObject]] else{return}
            for dict in dataArray{
                let model = CycleModel.deserialize(from: dict)
                self.cycleDatas.append(model!)
            }
            finishCallBack()
        }) { (response) in
            errorCallBack()
        }
    }
}
